﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Common.BaseLibrary.Authorization;
using Common.BaseLibrary.Utils;
using DepartmentWork.Migrator.Data;
using DepartmentWork.Platform.Controllers;
using DepartmentWork.Model;

namespace DepartmentWork.Platform.Areas.DepartmentArea.Controllers
{
    public class TrainController : BaseController
    {
        //
        // GET: /DepartmentArea/Train/

        public ActionResult Index()
        {

            TeamTrainViewModel model = new TeamTrainViewModel();

            #region 验证人员是否登录
            List<UserInfoViewModel> userList = new List<UserInfoViewModel>();

            if (CurrentUser.UserID.Trim().Length != 0)
            {
                Guid userId = new Guid(CurrentUser.UserID);
                Guid userTeamId =
                    userTeamsapceRepository.GetModel(r => r.USER_ID ==userId )
                        .FirstOrDefault()
                        .TEAMSPACE_ID;
                List<USER_TEAMSAPCE> userTeams = userTeamsapceRepository.GetModel(r => r.TEAMSPACE_ID == userTeamId).ToList();
                userList.AddRange((from item in userTeams
                                   select new UserInfoViewModel()
                                   {
                                       UserId = item.USER_ID,
                                   }).ToList());
                for (int i = 0; i < userList.Count(); i++)
                {
                    var id = userList[i].UserId;
                    userList[i].UserName =
                        userRepository.GetModel(r => r.USER_ID == id).FirstOrDefault().DISPLAY_NAME;
                }

                TempData["UserList"] = userList;
            }
            else
            {
                TempData["UserList"] = userList;
            }




            #endregion

            #region 设置分页

            var pageSize = this.Request["pageSize"] == null ? 10 : int.Parse(Request["pageSize"]);
            var pageIndex = this.Request["pageIndex"] == null ? 1 : int.Parse(Request["pageIndex"]);

            ViewData["pageIndex"] = pageIndex;
            ViewData["pageSize"] = pageSize;

            #endregion

            IList<TEAMTRAIN> list = teamTrainRepository.GetModel().OrderByDescending(r => r.TRAIN_TIME).Skip(pageSize * (pageIndex - 1)).Take(pageSize * pageIndex).ToList();

            DataSet ds = DataSetHelp.Instance.ToDataSet(list);

            model.TrainData = ds;

            ViewData["total"] = list.Count;





            return this.View(model);
        }

        [HttpPost]
        public ActionResult CreateTrain(FormCollection collection)
        {
            TEAMTRAIN model = new TEAMTRAIN();

            #region 获取界面数据

            model.TRAIN_TITLE = collection["tb_TrainTitle"];

            model.TRAIN_ADDRESS = collection["tb_TrainAddress"];

            model.TRAIN_DEPARTMENT = collection["tb_TrainDepartment"];

            model.TRAIN_TIME = Convert.ToDateTime(collection["tb_DeadTime"]);

            

            #endregion

            return this.RedirectToAction("Index");

        }

        [HttpPost]
        public ActionResult EditTrain(FormCollection collection)
        {
            return this.RedirectToAction("Index");
        }

        public JsonResult GetTrainInfo(Guid trainId)
        {
            return this.Json(true);
        }




    }
}
